<template>
    <div>
        <flexbox justify="flex-end" class="header-btn" v-if="!disabled && stepStatus !== 'requireOnline'" :key="detail">
            <a-button type="primary" @click="handleAdd" v-auth="'version.versionList.addRequire'"><PlusOutlined />添加需求</a-button>
            <span style="font-size: 14px; color: red; margin-left: 8px" v-if="formErrorMsg">{{ formErrorMsg }}</span>
        </flexbox>
        <!-- 外部系统 -->
        <div v-if="detail.jiraId">
            <RelateJiraIssue :stepStatus="stepStatus" :detail="detail" :disabled="disabled" @success="handleSuccess" />
        </div>
        <!-- 内部系统 -->
        <div v-else>
            <template v-for="(item, index) of comps" :key="index">
                <component
                    :is="item.comName"
                    v-bind="$attrs"
                    :detail="detail"
                    :disabled="disabled"
                    :stepStatus="stepStatus"
                    @success="handleSuccess"
                />
            </template>
        </div>
        <add-require ref="addRequireRef" @success="handleSuccess" />
    </div>
</template>

<script lang="ts">
import RelateJiraIssue from './RelateJIraIssue/index.vue';
import { PlusOutlined } from '@ant-design/icons-vue';
import AddRequire from '@/views/version/versionList/components/AddRequire/index.vue';
import RelateJira from '@/views/version/versionDetail/components/RelateRequire/RelateJira/index.vue';
import RelateRequireList from '@/views/version/versionDetail/components/RelateRequire/relateRequireList.vue';
import RelateProductList from '@/views/version/versionDetail/components/RelateRequire/relateProductList.vue';
import { defineComponent, ref, unref, reactive, toRefs, computed } from 'vue';
import { useJira } from '@/hooks/content/useJira';
export default defineComponent({
    components: {
        RelateJira,
        RelateRequireList,
        RelateProductList,
        AddRequire,
        PlusOutlined,
        RelateJiraIssue
    },
    props: {
        detail: { type: Object, default: () => ({}) },
        disabled: { type: Boolean, default: false },
        stepStatus: { type: String, default: '' }
    },
    emits: ['success'],
    setup(props, { emit }) {
        const { isLogin, checkLogin, loginJira } = useJira();
        const addRequireRef = ref<any>();
        const state = reactive<any>({
            formErrorMsg: ''
        });
        // 外采没有jira模块
        const comps = computed(() => {
            if (props.detail.systemType === '1') {
                return [{ comName: 'RelateProductList' }, { comName: 'RelateRequireList' }];
            } else {
                return [{ comName: 'RelateProductList' }, { comName: 'RelateRequireList' }, { comName: 'RelateJira' }];
            }
        });
        // 发起评审时必须
        const validate = () => {
            let res = false;
            if (props.detail.pmpProductDetailDTO.isOutsideSystem === '1') {
                res = props.detail.pmpJiraIssueDTOs ? !!props.detail.pmpJiraIssueDTOs.length : false;
            } else {
                res = props.detail.pmpProductRequires ? !!props.detail.pmpProductRequires.length : false;
            }
            state.formErrorMsg = res ? '' : '版本下产品需求不能为空';
            return Promise.resolve({
                res,
                errorMsg: res ? null : '版本下产品需求不能为空'
            });
        };
        // 添加需求
        const handleAdd = () => {
            const { processStatus } = props.detail;
            const query = {
                record: ['3', '4', '5', '6', '7', '8', '9'].includes(processStatus), // 789(验收，上线审核和上线流程)需要记录需求列表操作
                processStatus,
                taskNode: props.stepStatus
            };
            if (props.detail.pmpProductDetailDTO.isOutsideSystem === '1') {
                if (isLogin.value) {
                    unref(addRequireRef).open(props.detail, query);
                } else {
                    loginJira().then(() => {
                        handleAdd();
                    });
                }
            } else {
                unref(addRequireRef).open(props.detail, query);
            }
        };
        const handleSuccess = () => {
            emit('success', true);
        };
        if (props.detail.pmpProductDetailDTO) {
            if (props.detail.pmpProductDetailDTO.jiraSyncTmplDTO) {
                // 后台说他帮我弄好了
                checkLogin(props.detail.system);
            }
        }
        return {
            ...toRefs(state),
            comps,
            addRequireRef,
            validate,
            handleSuccess,
            handleAdd
        };
    }
});
</script>

<style scoped lang="less">
@import '../RelateProcess/CheckProcess/styles/index.less';
</style>
